package com.lv.mall.coupon.service.impl;

import com.lv.common.to.MemberPrice;
import com.lv.common.to.SkuReductionTo;
import com.lv.mall.coupon.entity.MemberPriceEntity;
import com.lv.mall.coupon.entity.SkuLadderEntity;
import com.lv.mall.coupon.service.MemberPriceService;
import com.lv.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lv.common.utils.PageUtils;
import com.lv.common.utils.Query;

import com.lv.mall.coupon.dao.SkuFullReductionDao;
import com.lv.mall.coupon.entity.SkuFullReductionEntity;
import com.lv.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // 1、保存满减打折，会员价
        /**
         * sku_ladder   阶梯价格 买几件打几折
         * sku_full_reduction   满减
         * member_price
         */
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());

        // 叠加其他优惠
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        // 折后价
//        skuLadderEntity.setPrice();
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }
//        skuLadderService.save(skuLadderEntity);
        // 2、满减
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0))==1) {
            this.save(skuFullReductionEntity);
        }

        // 3、会员价
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        // 是否有必要
        if (memberPrice != null && memberPrice.size() > 0) {
            List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
                MemberPriceEntity priceEntity = new MemberPriceEntity();
                priceEntity.setSkuId(skuReductionTo.getSkuId());
                priceEntity.setMemberLevelId(item.getId());
                priceEntity.setMemberLevelName(item.getName());
                priceEntity.setMemberPrice(item.getPrice());
                priceEntity.setAddOther(1); // 默认叠加其他优惠
                return priceEntity;
            }).filter(item->{
                return item.getMemberPrice().compareTo(new BigDecimal(0)) == 1;
            })// 过滤会员价格为0的情况
                    .collect(Collectors.toList());
            memberPriceService.saveBatch(collect);
        }
    }

}